package sitonya.entity.system

import com.IceCreamQAQ.Yu.annotation.EventListener
import com.icecreamqaq.yudb.jpa.annotation.Transactional
import sitonya.concat.BOT_ID
import javax.inject.Inject

@EventListener
class BotSwitchService {
    @Inject
    lateinit var iSwitch: ISwitch

    @Transactional
    fun setSwitch(groupId: Long, enabled: Boolean = true) {
        val switch = iSwitch.findById(groupId, BOT_ID)?.apply {
            this.enabled = enabled
        } ?: Switch(groupId = groupId, enabled = enabled)
        iSwitch.saveOrUpdate(switch)
    }

    @Transactional
    fun getSwitch(groupId: Long): Boolean {
        val switch = iSwitch.findById(groupId, BOT_ID) ?: let {
            val tmp = Switch(groupId = groupId)
            iSwitch.saveOrUpdate(tmp)
            tmp
        }
        return switch.enabled
    }
}